package com.hzw.jdbc.app;

import com.hzw.utils.JdbcUtil;

import java.sql.*;
import java.util.Scanner;

public class LoginDemo01 {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入账号：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        login1(username,password);
    }

    public static void login(String username,String password) throws SQLException {
        System.out.print("username:"+username);
        System.out.println("    password:"+password);
        String sql="SELECT id FROM `user` WHERE `username`='"+username+"' AND `password`=MD5('"+password+"')";
        System.out.println(sql);
        Connection connection = JdbcUtil.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if(resultSet.next()){
            System.out.println("登入成功 id为"+resultSet.getString(1));
        }else {
            System.out.println("登入失败哦");
        }
        JdbcUtil.release(connection,statement,resultSet);
    }

    public static void login1(String username,String password) throws SQLException {
        System.out.print("username:"+username);
        System.out.println("    password:"+password);
        String sql="SELECT id FROM `user` WHERE `username`=? AND `password`=MD5(?)";
        Connection connection = JdbcUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        ResultSet resultSet = ps.executeQuery();
        if(resultSet.next()){
            System.out.println("登入成功 id为"+resultSet.getString(1));
        }else {
            System.out.println("登入失败哦");
        }
        JdbcUtil.release(connection,ps,resultSet);
    }


}
